{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.6.7'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from platform import python_version\n",
    "\n",
    "python_version()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {'foo':10,'bar':30,'baz':20}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "foo => 10\n",
      "bar => 30\n",
      "baz => 20\n"
     ]
    }
   ],
   "source": [
    "for key,val in d.items():\n",
    "    print('{} => {}'.format(key,val))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'foo': 1, 'bar': 2}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "allowed_values = [1, 2]\n",
    "\n",
    "d = {'foo':1, 'bar':2, 'baz':3, 'quux':4 }\n",
    "\n",
    "filtered_items = [(k,v) for (k,v) in d.items() if v in allowed_values]\n",
    "\n",
    "dict(filtered_items)\n",
    "# >> {'bar': 2, 'foo': 1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('foo', 1), ('bar', 2), ('baz', 3), ('quux', 4)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(d.items(),key=lambda x:x[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('bar', 2), ('baz', 3), ('foo', 1), ('quux', 4)]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted(d.items(),key=lambda x:x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "foo => 10\n",
      "baz => 20\n",
      "bar => 30\n"
     ]
    }
   ],
   "source": [
    "d = {'foo':10, 'bar':30,'baz':20}\n",
    "\n",
    "sorted_list_of_tuples = sorted(d.items(),key=lambda x:x[1])\n",
    "\n",
    "od = OrderedDict(sorted_list_of_tuples)\n",
    "\n",
    "for key,val in od.items():\n",
    "    print('{} => {}'.format(key,val))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## merge dicts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "from collections import ChainMap\n",
    "\n",
    "d1 = {'a':1, 'b': 2}\n",
    "\n",
    "d2 = {'b':100, 'c': 200}\n",
    "\n",
    "d3 = ChainMap({}, d1, d2)\n",
    "\n",
    "print(d3[\"a\"])\n",
    "print(d3[\"b\"])\n",
    "print(d3[\"c\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## dict comprehension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "foo => 10\n",
      "bar => 32\n",
      "baz => 32\n"
     ]
    }
   ],
   "source": [
    "d = dict()\n",
    "\n",
    "d['foo'] = 10\n",
    "d['bar'] = 32\n",
    "d['baz'] = 32\n",
    "\n",
    "for key,val in d.items():\n",
    "    print('{} => {}'.format(key,val))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "foo => 10\n",
      "bar => 32\n",
      "baz => 5\n"
     ]
    }
   ],
   "source": [
    "od = OrderedDict()\n",
    "\n",
    "od['foo'] = 10\n",
    "od['bar'] = 32\n",
    "od['baz'] = 5\n",
    "\n",
    "for key,val in od.items():\n",
    "    print('{} => {}'.format(key,val))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6",
   "language": "python",
   "name": "python36"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
